﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HokjesSluiten
{
    public class Bot
    {
        public StreamReader output;

        public StreamWriter input;

        public string botName { get; set; }

        private Process process;

        public Bot(string name)
        {
            botName = name; 
        }

        public Process playerProcess
        {
            get { return process; }
            set
            {
                process = value;
                output = process.StandardOutput;
                input = process.StandardInput;
            }
        }

        public void start(string processFileName)
        {
            Process process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = processFileName,
                    UseShellExecute = false,
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = false,
                }
            };

            process.Start();
            playerProcess = process;
        }

        public void talkTo(string move)
        {
            input.WriteLine(move);
            input.Flush();
        }

        public string listenTo()
        {
            string commandLine = string.Empty;

            if (output.EndOfStream) return commandLine;
            commandLine = output.ReadLine();
            output.DiscardBufferedData();

            return commandLine;
        }
    }
}
